<?php

class Model_car extends CI_Model{
	function __construct(){
        parent::__construct();
	}
	
	/*
			=================Multi Purposes Function=================
	*/
	
	function getOwnerList(){
		$this->db->select("*");
		$this->db->from("owner");
		$this->db->where("jenis", "agency");
		$this->db->order_by("owner_name asc");
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	
	
	function getOwnerListLsg(){
		$this->db->select("*");
		$this->db->from("owner");
		$this->db->where("jenis", "leasing");
		$this->db->order_by("owner_name asc");
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	
	function getPaket(){
		$this->db->select("*");
		$this->db->from("mdf");
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	/*
			=================Function Add Rent Car=================
	*/
	
	function addRent($status='mobil masih disewa'){
		$id_car = $this->model_car->get_idCar(	$this->input->post("plat_mobil",true),
													$this->input->post("nama_mobil",true),
													$this->input->post("nama_pemilik",true)
													);
		$id_orang = 0;
		$data = array(
		   'harga' => $this->input->post("harga_sewa",true) ,
		   'car_id' => $id_car ,
		   'tgl_mulai' => date($this->input->post("tanggal_pinjam", true)) ,
		   'tgl_akhir' => date($this->input->post("tanggal_kembali", true))
		   
		);
		$this->db->insert('sewa', $data); 
	}
	
	
	function get_idCar($plat, $mobil, $pemilik){
		$masa_stnk = $this->input->post("thn", true) . "-" . $this->input->post("bln", true) . "-01";
		$this->db->select("car_id");
		$this->db->from("car");
		$this->db->where("car_license", $plat);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$query2 = $query->result_array();
			$hasil = $query2[0]['car_id'];
			return $hasil;
		}else{
			$data = array(
		   		'car_name' => $this->input->post("nama_mobil",true),
				'car_license'=> $this->input->post("plat_mobil",true),
				'tarif' => $this->input->post("tarif",true),
				'warna'=> $this->input->post("warna",true),
				'produsen' => $this->input->post("produsen",true),
				'type' => $this->input->post("tipe",true),
				'model' => $this->input->post("model",true),
				'daya' => $this->input->post("daya",true),
				'mesin' => $this->input->post("mesin",true),
				'transmisi' => $this->input->post("transmisi",true),
				'bensin' => $this->input->post("bensin",true),
				'masa_stnk' => $masa_stnk,
				'pembuatan' => $this->input->post("tahun",true),
				'owner_id' => $this->input->post("nama_pemilik",true),
				'kepemilikan' => "leasing"
			);
			$this->db->insert('car', $data); 
			$id = $this->db->insert_id('owner', $data);
			$nid = "CAR" . $id;
			$data = array(
				'nic' => $nid
			);
			$this->db->where("car_id",$id);
			$this->db->update('car', $data);
			return $id;
		}
	}
	
	
	function getDate($plat){
		$car_id = $this->model_car->getId($plat);
		$this->db->select("*");
		$this->db->from("sewa");
		$this->db->where("car_id", $car_id);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}
		else{
			return false;	
		}
	}
	
	function getId($plat1){
		$this->db->select("car_id");
		$this->db->from("car");
		$this->db->where("car_license", $plat1);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$query2 = $query->result_array();
			$hasil = $query2[0]['car_id'];
			return $hasil;
		}
	}
	
	function getProdusen(){
		$this->db->select("nama");
		$this->db->from("car_produsen");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$query2 = $query->result_array();
			return $query2;
		}
	}
	/*
			=================Function Add Leasing Car=================
	*/
	function addLsg(){
		$masa_stnk = $this->input->post("thn", true) . "-" . $this->input->post("bln", true) . "-" . $this->input->post("tgl", true);
		$data = array(
		   		'car_name' => $this->input->post("nama_mobil",true),
				'car_license'=> $this->input->post("plat_mobil",true),
				'warna'=> $this->input->post("warna",true),
				'produsen' => $this->input->post("produsen",true),
				'type' => $this->input->post("tipe",true),
				'model' => $this->input->post("model",true),
				'daya' => $this->input->post("daya",true),
				'mesin' => $this->input->post("mesin",true),
				'transmisi' => $this->input->post("transmisi",true),
				'bensin' => $this->input->post("bensin",true),
				'masa_stnk' => $masa_stnk,
				'pembuatan' => $this->input->post("tahun",true),
				'owner_id' => $this->input->post("nama_pemilik",true),
				'kepemilikan' => "leasing"
				
			);
		$this->db->insert('car', $data); 
		$id = $this->db->insert_id('owner', $data);
		$nid = "CAR" . $id;
		$data = array(
			'nic' => $nid
		);
		$this->db->where("car_id",$id);
		$this->db->update('car', $data);
		
		$data = array(
		   		'carprice_id' => $id,
				'price'=> $this->input->post("tarif",true),
				'kode'=> "CDR-" . date("ym") . $id,
				'keterangan' => "Harian"
				
			);
		$this->db->insert('car_price', $data); 
			$data = array(
		   		'carprice_id' => $id,
				'price'=> $this->input->post("tarifminggu",true),
				'kode'=> "CWR-" . date("ym") . $id,
				'keterangan' => "Mingguan"
				
			);
		$this->db->insert('car_price', $data);
			$data = array(
		   		'carprice_id' => $id,
				'price'=> $this->input->post("tarifbulan",true),
				'kode'=> "CMR-" . date("ym") . $id,
				'keterangan' => "Bulanan"
				
			);
		$this->db->insert('car_price', $data);
			$data = array(
		   		'carprice_id' => $id,
				'price'=> $this->input->post("tarif6bulan",true),
				'kode'=> "C6MR-" . date("ym") . $id,
				'keterangan' => "Enam Bulanan"
				
			);
		$this->db->insert('car_price', $data);
		
			$data = array(
		   		'carprice_id' => $id,
				'price'=> $this->input->post("tariftahun",true),
				'kode'=> "CYR-" . date("ym") . $id,
				'keterangan' => "Tahunan"
				
			);
		$this->db->insert('car_price', $data);
		
			$data = array(
		   		'carprice_id' => $id,
				'price'=> $this->input->post("tarifmdf",true),
				'kode'=> "CDF-" . date("ym") . $id,
				'keterangan' => "Paket CDF"
				
			);
		$this->db->insert('car_price', $data);
		
		$new_format_number = number_format($this->input->post("mdfsupir",true), 0, '', ''); 
		$new_format_numberfuel = number_format($this->input->post("mdffuel",true), 0, '', ''); 
			$data = array(
		   		'supir' => $new_format_number,
				'fuel'=> $new_format_numberfuel
				
				
			);
			$this->db->where("id",1);
		$this->db->update('mdf', $data);
		
	}	
	function checkunique($field,$str){
		$this->db->select("*");
		$this->db->from("car");
		$this->db->where($field,$str);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return false;
		}else{
			return true;
		}
	}
	
	
	/*
			=================Function View Car=================
	*/
	
	function getCarList(){
		$this->db->from('car', 'owner');
		$this->db->join('owner', 'owner.owner_id = car.owner_id');
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function ajaxGetSearch($plat){
		$this->db->from('car', 'owner');
		$this->db->join('owner', 'owner.owner_id = car.owner_id');
		$this->db->like("car_license", $plat,"after");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function ajaxGetCarList($id, $mode){
		$this->db->select("*");
		$this->db->from("car");
		if($id!="X")
		{
			$this->db->where("owner_id", $id);
		}
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	/*
			=================Function Edit Car=================
	*/
	function getCarDet($id){
		$this->db->select("*");
		$this->db->from("car");
		$this->db->join("owner", "owner.owner_id=car.owner_id");
		$this->db->where("car_id",$id);
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function getCarPrice($id){
		$this->db->select("*");
		$this->db->from("car_price");
		//$this->db->join("owner", "owner.owner_id=car.owner_id");
		$this->db->where("carprice_id",$id);
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function editRent($id){
		$masa_stnk = $this->input->post("thn", true) . "-" . $this->input->post("bln", true) . "-" . $this->input->post("tgl", true);
		$data = array(
		   'car_name' => $this->input->post("nama_mobil",true),
			'car_license'=> $this->input->post("plat_mobil",true),
			'tarif' => $this->input->post("tarif",true),
			'warna'=> $this->input->post("warna",true),
			'produsen' => $this->input->post("produsen",true),
			'type' => $this->input->post("tipe",true),
			'model' => $this->input->post("model",true),
			'daya' => $this->input->post("daya",true),
			'mesin' => $this->input->post("mesin",true),
			'transmisi' => $this->input->post("transmisi",true),
			'bensin' => $this->input->post("bensin",true),
			'masa_stnk' => $masa_stnk,
			'pembuatan' => $this->input->post("tahun",true),
			'owner_id' => $this->input->post("nama_pemilik",true)
		);
		$this->db->where('car_id', $id);
		$this->db->update('car', $data); 
	}
	
	/*
			=================Function Delete Car=================
	*/	
	function delDataCar($id){
		$this->db->where('car_id', $id);
		$this->db->delete('car'); 
	}
	
	/*
			=================Function ViewDetail=================
	*/
	function addProdusen($bookn){
		$datax = array(
		   	'nama' =>$bookn
		);
		$this->db->insert('car_produsen', $datax); 
			
	}
	
}